package com.boot.rabbitmq.apidemo.model.subscribe;

import com.boot.rabbitmq.apidemo.model.util.ConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 *      订阅模式,向交换机中添加消息
 */
public class Send {
    private final static String EXCHANGE_NAME = "test_exchange_fanout";

    public static void sen(String message) throws Exception {
        // 获取到连接以及mq通道
        Connection connection = ConnectionUtil.getConnection();
        Channel channel = connection.createChannel();

        // 声明exchange
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

        // 消息内容
        channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");

        channel.close();
        connection.close();
    }

    public static void main(String[] args) {
        for (int i=100;i>0;i--){
            try {
                sen("message_"+i);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

